package com.quest.parallelDesign;

import java.util.ArrayList;
import java.util.concurrent.atomic.LongAdder;

/**
 * Created by Quest on 2018/10/9.
 */
public class ParallelStream {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();

        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i = 1; i <= 100; i++) {
            list.add(i);
        }
        LongAdder sum = new LongAdder();
        //并行执行，适用于内部执行时间较长的循环
        list.parallelStream().forEach(integer -> {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            sum.add(integer);
        });
        //串行执行，适用于内部简单，执行时间很短的循环
        /*for (Integer i : list) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            sum.add(i);
        }*/
        System.out.println(sum+" ,总耗时："+(System.currentTimeMillis()-start));
    }

}
